// execute when form is submitted and document ready
$(document).ready(function() {
	$('form#contactForm').submit(function() {
		return false;
	});
});

// hide error messages and validate required fields class of "requiredField"
var hasError = false;
$('.requiredField').each(function() {
	
});

// if the field is not empty create error message
if(jQuery.trim($(this).val()) == '') {
	var labelText = $(this).prev('label').text();
	$(this).parent().append('<span class="error">You forgot to enter your '+labelText+'.</span>');
	hasError = true;
	}
	
// if the field is not empty and has class of email, validate email address
else if($(this).hasClass('email')) {
	var emailReg = /^([w-.]+@([w-]+.)+[w-]{2,4})?$/;
	if(!emailReg.test(jQuery.trim($(this).val()))) {
		var labelText = $(this).prev('label').text();
		$(this).parent().append('<span class="error">You entered an invalid '+labelText+'.</span>');
		hasError = true;
	}
}

// no errors, replace submit button with loading image
if(!hasError) {
	$('form#contactForm li.buttons button').fadeOut('normal', function() {
		$(this).parent().append('<img src="/wp-content/themes/spartans-speak/images/loading.gif" alt="Loading..." height="31" width="31" />');
	});
}

// submit via AJAX request, slide form up and show thank you message
var formInput = $(this).serialize();
$.post($(this).attr('action'), formInput, function(data) {
	$('form#contactForm').slideUp("fast", function() {
		$(this).before('<p class="thanks"><strong>Thanks!</strong> Your email was successfully sent. We will be in touch very soon.</p>');
	});
}); 